package cn.edu.zhku.citrusmonitor.baseinfo.citrus.service;

import java.io.File;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.edu.zhku.citrusmonitor.baseinfo.citrus.dao.CitrusDao;
import cn.edu.zhku.citrusmonitor.baseinfo.citrus.dao.CitrusDaoImpl;
import cn.edu.zhku.citrusmonitor.baseinfo.citrus.exception.CitrusException;
import cn.edu.zhku.citrusmonitor.domain.Bean;
import cn.edu.zhku.citrusmonitor.domain.TbCitrus;
import cn.edu.zhku.citrusmonitor.util.PageBean;

public class CitrusServiceImpl implements CitrusService{

	private CitrusDao citrusDao = new CitrusDaoImpl();
	
	@Override
	public boolean addCitrus(TbCitrus citrus) throws CitrusException{
		try {
			int effectedNum = citrusDao.insertCitrus(citrus);
			if (effectedNum > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("添加失败，请重试!");
		}
		return false;
	}

	@Override
	public boolean removeCitrus(String citrusId) throws CitrusException {
		try {
			String absolutePath = citrusDao.selectFilePathById(citrusId);
			int effectedNum = 0;
			if(absolutePath == null) {
				effectedNum = citrusDao.deleteCitrus(citrusId);
				if (effectedNum > 0) {
					return true;
				}
			}else {
				File file = new File(absolutePath);
				effectedNum = citrusDao.deleteCitrus(citrusId);
				if (effectedNum > 0) {
					if(file.exists()) {
						file.delete();
					}
					return true;
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("删除信息失败，请重试!");
		}
		return false;
	}

	@Override
	public boolean modifyCitrus(TbCitrus citrus) throws CitrusException {
		try {
			int effectedNum = citrusDao.updateCitrus(citrus);
			if(effectedNum > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("更新信息失败，请重试!");
		}
		return false;
	}

	@Override
	public TbCitrus findCitrusById(String citrusId) throws CitrusException {
		try {
			return citrusDao.selectCitrusById(citrusId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("查询信息失败，请重新!");
		}
	}

	@Override
	public boolean setCitrusTnlg(String citrusId, String technologyId) throws CitrusException {
		try {
			int effectedNum = citrusDao.updateCitrusInTnlg(citrusId, technologyId);
			if(effectedNum > 0) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("设置失败，请重试!");
		}
		return false;
	}

	@Override
	public PageBean<TbCitrus> findCitrusByNameLike(int pageSize, int currentPage, String citrusName) throws CitrusException {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("citrus_name", citrusName);
		try {
			return fySearchByCondition(pageSize, currentPage, condition);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("查询信息失败，请重新!");
		}
	}

	@Override
	public PageBean<TbCitrus> fySearchAll(int pageSize, int currentPage) throws CitrusException {
		try {
			return fySearchByCondition(pageSize, currentPage, null);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("查询信息失败，请重新!");
		}
	}
	
	private PageBean<TbCitrus> fySearchByCondition(int pageSize, int currentPage, Map<String, Object> condition) throws SQLException {
		PageBean<TbCitrus> pageBean = new PageBean<TbCitrus>();
		int count = citrusDao.countList(condition);
		List<TbCitrus> citrusList = citrusDao.selectCitrusList(pageSize, currentPage, condition);
		pageBean.setCurrentPage(currentPage);
		pageBean.setList(citrusList);
		pageBean.setCount(count);
		pageBean.setPageSize(pageSize);
		pageBean.setTotalPage((int)Math.ceil(count*1.0/pageSize));
		return pageBean;
	}

	@Override
	public List<TbCitrus> findCitrusesLimit(int size) throws CitrusException {
		try {
			return citrusDao.selectCitrusesLimit(size);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("加载信息失败，请重新!");
		}
	}

	@Override
	public PageBean<Bean> findPageList(int pageSize, int currentPage) throws CitrusException {
		PageBean<Bean> pageBean = new PageBean<Bean>();
		int count = 0;
		try {
			count = citrusDao.countList(null);
			List<Bean> citrusList = citrusDao.selectCitrusList(pageSize, currentPage);
			pageBean.setCurrentPage(currentPage);
			pageBean.setList(citrusList);
			pageBean.setCount(count);
			pageBean.setPageSize(pageSize);
			pageBean.setTotalPage((int)Math.ceil(count*1.0/pageSize));
			return pageBean;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("加载信息失败，请重新!");
		}
	}

	@Override
	public Bean getCitrusById(String citrusId) throws CitrusException {
		try {
			return citrusDao.getCitrusById(citrusId);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("加载信息失败，请重新!");
		}
	}

	@Override
	public boolean removeCitrusesByIds(String[] ids) throws CitrusException {
		Object[][] params = new Object[ids.length][];
		for(int i=0;i<ids.length;i++) {
			params[i] = new Object[] {ids[i]};
		}
		try {
			int res = citrusDao.deleteCitrusesByIds(params);
			if(res == ids.length) {
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			throw new CitrusException("删除失败，请重试!");
		}
		return false;
	}
	
}
